<?php
namespace Resmodel;



use Core\Constant;
use Resmodel\Factory\ResTableGatewayFactory;
use Resmodel\ResAbstractModel;

class Dish extends ResAbstractModel
{
    public $id;
    public $created_date;
    public $active;

    private $_dishLangArr = array();

    /**
     * @param $lang
     * @return DishLang|null
     */
    public function getDishLang($lang){
        if(!$this->_dishLangArr[$lang]){
            $dishLangTable = ResTableGatewayFactory::getDishLangTable();
            $dishLangTable->setLang($lang);
            $this->_dishLangArr[$lang] = $dishLangTable->getByDishId($this->id);
        }

        return $this->_dishLangArr[$lang];
    }

    public function getName($lang){
        return $this->getDishLang($lang)->name;
    }

    public function getDescription($lang){
        return $this->getDishLang($lang)->description;
    }

    public function getContent($lang){
        return $this->getDishLang($lang)->content;
    }

    public function getHowTo($lang){
        return $this->getDishLang($lang)->how_to;
    }

    public function getIngredient($lang){
        return $this->getDishLang($lang)->ingredient;
    }

    public function getDishImages(){
        $dishImageTable = ResTableGatewayFactory::getDishImageTable();
        return $dishImageTable->getAllImageByDishId($this->id);
    }

    public function getDishPrice(){
        $dishPriceTable = ResTableGatewayFactory::getDishPriceTable();
        return $dishPriceTable->getAllPriceByDishId($this->id);
    }

    public function getDishCategory(){
        $dishCategoryTable = ResTableGatewayFactory::getDishCategoryTable();
        return $dishCategoryTable->getAllCategoryByDishId($this->id);
    }

    public function getDishCategoryByGroup($lang, $categoryGroup){
        $dishCategoryTable = ResTableGatewayFactory::getDishCategoryTable();
        return $dishCategoryTable->getAllCategoryByDishIdAndGroup($this->id,$lang, $categoryGroup);
    }

    public function getDishTags($lang){
        $dishTagTable = ResTableGatewayFactory::getDishTagTable($lang);
        return $dishTagTable->getAllTagsByDishId($this->id);
    }

    public function getDishTagsAsString($lang){
        $allTagRS = $this->getDishTags($lang);
        $tmpStr = '';
        if($allTagRS)
            foreach ($allTagRS as $aTag) {
                $tmpStr = $tmpStr.','.$aTag->tag;
            }
        if(strlen($tmpStr) > 0)
            $tmpStr = substr($tmpStr, 1);
        return $tmpStr;

    }

    public function getDishArticles($lang){
        $dishArticleTable = ResTableGatewayFactory::getDishArticleTable($lang);
        return $dishArticleTable->getAllArticleByDishId($this->id);
    }

    public function detachCategories()
    {
        $dishCategoryTable = ResTableGatewayFactory::getDishCategoryTable();
        $allCategories = $this->getDishCategory();
        if($allCategories)
            foreach ($allCategories as $aCategory) {
                $dishCategoryTable->getTableGateway()->delete(
                    array('dish_id'=>$this->id, 'category_id'=>$aCategory->id));
            }
    }

    public function detachTags($lang)
    {
        $dishTagTable = ResTableGatewayFactory::getDishTagTable($lang);
        $dishTagTable->getTableGateway()->delete(array('dish_id'=>$this->id,
            'lang'=>$lang));
    }

    public function detachPrices()
    {
        $dishPriceTable = ResTableGatewayFactory::getDishPriceTable();
        $dishPriceTable->getTableGateway()->delete(array('dish_id'=>$this->id));
    }

    public function detachArticles($lang)
    {
        $dishArticlesTable = ResTableGatewayFactory::getDishArticleTable($lang);
        $dishArticlesTable->getTableGateway()->delete(array('dish_id'=>$this->id,
            'lang'=>$lang));
    }

    public function detachImages()
    {
        $dishImagesTable = ResTableGatewayFactory::getDishImageTable();
        $dishImagesTable->getTableGateway()->delete(array('dish_id'=>$this->id));
    }

    /**
     * @return Category || null
     */
    public function getPrimaryCategory(){
        $arrCat = $this->getCategoryByGroupCode(Constant::$CATEGORY_GROUP_DISH);
        if ($arrCat != null && $arrCat)
            foreach ($arrCat as $aCat) {
                if($aCat->is_primary == 1)
                    return $aCat;
            }

    }

    /**
     * @return array
     */
    public function getSubCategories(){
        $categoryTable = ResTableGatewayFactory::getMlCategoryTable();
        $select = $categoryTable->getDbSqlSelect();
        $select->join(array('ur' => 'dish_category'), 'ur.category_id = ml_category.id', array())
            ->where(array('ur.dish_id' => $this->id, 'ur.is_primary' => 0));
        $rsi = $categoryTable->getTableGateway()->selectWith($select);
        $resultArr = array();
        foreach ($rsi as $aCategory) {
            array_push($resultArr, $aCategory);
        }

        return $resultArr;
    }

    /**
     * @param $groupCode
     * @return array
     */
    public function getCategoryByGroupCode($groupCode)
    {
        $categoryTable = ResTableGatewayFactory::getMlCategoryTable();
        $select = $categoryTable->getDbSqlSelect();
        $select->join(array('ur' => 'dish_category'), 'ur.category_id = ml_category.id', array())
            ->where(array('ur.dish_id' => $this->id, 'ml_category.group_code' => $groupCode));
        $rsi = $categoryTable->getTableGateway()->selectWith($select);
        $resultArr = array();
        foreach ($rsi as $aCategory) {
            array_push($resultArr, $aCategory);
        }

        return $resultArr;
    }



}
